package com.alib.springcloud.controller;
import com.alib.springcloud.service.OrderService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
@RestController
public class SentinelCtroller {

    // 流控模式
    @GetMapping("/api/rumen")
    public String rumen(){
        return Thread.currentThread().getName() + " == rumen ==";
    }

    @GetMapping("/api/guanlian1")
    public String guanlian1(){
        return Thread.currentThread().getName() + " == guanlian1 ==";
    }
    @GetMapping("/api/guanlian2")
    public String guanlian2(){
        return Thread.currentThread().getName() + " == guanlian2 ==";
    }
    //链路模式：只统计从/order/lianlu1进入/goods的资源，QPS阈值为5，超出则被限流，有点限制 service层那味
    @Autowired
    private OrderService orderService;
    @GetMapping("/api/lianlu1")
    public String lianlu1(){
        orderService.queryGoods();
        return Thread.currentThread().getName() + " == lianlu1 ==";
    }
    @GetMapping("/api/lianlu2")
    public String lianlu2(){
        orderService.queryGoods();
        return Thread.currentThread().getName() + " == lianlu2 ==";
    }

    //流控效果
    @GetMapping("/api/warmup")
    public String warmup(){
        return Thread.currentThread().getName() + " == warmup ==";
    }
    @GetMapping("/api/paiduidengdai")
    public String paiduidengdai(){
        return Thread.currentThread().getName() + " == paiduidengdai ==";
    }


    //热点key
    @GetMapping("/api/redian")
    @SentinelResource(value = "redian") // 这里添加，sentinel才会显示资源名字
    public String redian( @RequestParam(value = "id",required = false) Long id, @RequestParam(value = "id",required = false) Long id2){
        return Thread.currentThread().getName() + " == paiduidengdai ==";
    }

    //服务熔断
    @GetMapping("/api/rongduan")
    public String rongduan(){
        try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); }
        return Thread.currentThread().getName() + " == rongduan ==";
    }

}
